import java.util.*;
import java.io.*;

import jade.lang.acl.*;
import jade.wrapper.AgentController;
import jade.wrapper.StaleProxyException;
import jade.content.*;
import jade.content.onto.basic.*;
import jade.content.lang.*;
import jade.content.lang.sl.*;
import jade.core.*;
import jade.core.behaviours.*;
import jade.domain.*;
import jade.domain.mobility.*;
import jade.domain.JADEAgentManagement.*;
import jade.gui.*;

public class MinhControllerAgent extends Agent {
	private jade.wrapper.AgentContainer home;
	private jade.wrapper.AgentContainer[] container = null;
	private Map locations = new HashMap();
	private Vector agents = new Vector();
	private int agentCnt = 0;
	private int command;

	public static final int QUIT = 0;
	public static final int NEW_AGENT = 1;
	public static final int MOVE_AGENT = 2;
	public static final int CLONE_AGENT = 3;
	public static final int KILL_AGENT = 4;

	jade.core.Runtime runtime = jade.core.Runtime.instance();

	protected void setup() {
		getContentManager().registerLanguage(new SLCodec());
		getContentManager().registerOntology(MobilityOntology.getInstance());
		try {
			home = runtime.createAgentContainer(new ProfileImpl());
			container = new jade.wrapper.AgentContainer[3];
			for (int i = 0; i < 2; i++) {
				container[0] = runtime.createAgentContainer(new ProfileImpl());
			}

			sendRequest(new Action(getAMS(), new QueryPlatformLocationsAction()));

			// Receive response from AMS
			MessageTemplate mt = MessageTemplate.and(
					MessageTemplate.MatchSender(getAMS()),
					MessageTemplate.MatchPerformative(ACLMessage.INFORM));
			ACLMessage resp = blockingReceive(mt);
			ContentElement ce = getContentManager().extractContent(resp);
			Result result = (Result) ce;
			jade.util.leap.Iterator it = result.getItems().iterator();
			while (it.hasNext()) {
				Location loc = (Location) it.next();
				locations.put(loc.getName(), loc);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		myAgent.doMove((Location) locations.get("Container-1"));
		AID cura= new AID("Curator", AID.ISLOCALNAME);
		createCloneAndMove( cura,"Container-1",1);
		  
		try {
			   startNewAgent("CuratorAgent", cura.getLocalName(),
			     null);
		}catch (StaleProxyException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}

	private void startNewAgent(String className, String agentName,
			Object[] arguments) throws StaleProxyException {

		((AgentController) getContainerController().createNewAgent(agentName,
				className, arguments)).start();
	}

	private void createCloneAndMove(AID aid, String destName, int id) {
		// set location
		Location dest = (Location) locations.get(destName);

		// set mobile agent description
		MobileAgentDescription mad = new MobileAgentDescription();
		mad.setName(aid);
		this.doMove(dest);
		mad.setDestination(dest);
		CloneAction ca = new CloneAction();
		ca.setNewName("Clone-" + aid.getLocalName() + id);
		ca.setMobileAgentDescription(mad);

		// sends a request about making a new copy of this agent
		sendRequest(new Action(getAID(), ca));
	}

	void sendRequest(Action action) {
		// ---------------------------------

		ACLMessage request = new ACLMessage(ACLMessage.REQUEST);
		request.setLanguage(new SLCodec().getName());
		request.setOntology(MobilityOntology.getInstance().getName());
		try {
			getContentManager().fillContent(request, action);
			request.addReceiver(action.getActor());
			send(request);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}
